package com.yhsoft.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.bill.controller.request.SalerSkInVo;
import com.yhsoft.bill.controller.request.SalerYszkMxInVo;
import com.yhsoft.bill.controller.response.SalerYszkMxOutVo;
import com.yhsoft.bill.entity.SalerYszkMx;
import com.yhsoft.bill.mapper.SalerYszkMxMapper;
import com.yhsoft.bill.service.ISalerYszkMxService;
import com.yhsoft.common.service.TokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * <p>
 * 渠道商应收账款明细 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-03
 */
@Service
public class SalerYszkMxServiceImpl extends ServiceImpl<SalerYszkMxMapper, SalerYszkMx> implements ISalerYszkMxService {

    @Override
    public void addYfmx(Integer customerId, BigDecimal multiply, String firstBatchNum, String secondBantchNum) {
        SalerYszkMx mx = new SalerYszkMx();
        mx.setCustomerId(customerId);
        mx.setYsAmount(multiply);
        mx.setPayAmount(BigDecimal.ZERO);
        mx.setFirstBatchNum(firstBatchNum);
        mx.setSecondBantchNum(secondBantchNum);
        mx.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        baseMapper.insert(mx);
    }

    @Override
    public IPage<SalerYszkMxOutVo> getYszkMx(SalerYszkMxInVo inVo, Integer customerId, Long pageNo, Long pageSize) {
        LambdaQueryWrapper<SalerYszkMx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SalerYszkMx::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        queryWrapper.eq(SalerYszkMx::getCustomerId, customerId);
        queryWrapper.like(StringUtils.hasLength(inVo.getPayBy()), SalerYszkMx::getPayBy, inVo.getPayBy());
        queryWrapper.eq(StringUtils.hasLength(inVo.getFirstBatchNum()), SalerYszkMx::getFirstBatchNum, inVo.getFirstBatchNum());
        queryWrapper.eq(StringUtils.hasLength(inVo.getSecondBantchNum()), SalerYszkMx::getSecondBantchNum, inVo.getSecondBantchNum());
        queryWrapper.ge(inVo.getStartTime() != null, SalerYszkMx::getPayTime, inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            queryWrapper.le(SalerYszkMx::getPayTime, LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }

        queryWrapper.orderByDesc(SalerYszkMx::getCreateTime);
        Page<SalerYszkMx> page = new Page<>(pageNo, pageSize);
        IPage<SalerYszkMx> iPage = baseMapper.selectPage(page, queryWrapper);

        IPage<SalerYszkMxOutVo> convert = iPage.convert(SalerYszkMx -> {
            SalerYszkMxOutVo form = new SalerYszkMxOutVo();
            BeanUtils.copyProperties(SalerYszkMx, form);
            return form;
        });
        return convert;
    }

    @Override
    public String sk(SalerSkInVo inVo) {
        SalerYszkMx yszkMx = this.baseMapper.selectById(inVo.getId());
        if(yszkMx == null){
            return "收款失败！未查询到应收账款详情记录！";
        }
        yszkMx.setPayAmount(inVo.getPayAmount());
        yszkMx.setVoucher(inVo.getVoucher());
        yszkMx.setPayBy(TokenService.getUserBytoken().getName());
        yszkMx.setPayTime(LocalDateTime.now());
        this.baseMapper.updateById(yszkMx);
        return "success";
    }

}
